﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace SimpleTaskManager.Domain.Entities
{
    /// <summary>
    /// The class to represent a user in the system
    /// </summary>
    public class User
    {
        /// <summary>
        /// ID field
        /// </summary>
        public int UserID { get; set; }

        /// <summary>
        /// The user's login name
        /// </summary>
        [Required(ErrorMessage="Please enter a user login")]
        [StringLength(20, MinimumLength=2)]
        [Display(Name = "Login")]
        public string Login { get; set; }

        /// <summary>
        /// The hash of the user's password
        /// </summary>
        [Required(ErrorMessage="Please enter a password")]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        /// <summary>
        /// The backing property for the Role which will be present in the database
        /// </summary>
        public int RoleRep
        {
            get { return (int)Role; }
            set { Role = (UserRole)value; }
        }

        /// <summary>
        /// The role which describes the permissions of the user
        /// </summary>
        public UserRole Role { get; set; }

        /// <summary>
        /// The list of the user's skills
        /// </summary>
        [InverseProperty("User")]
        public virtual ICollection<User_Skill> User_Skills { get; set; }

        /// <summary>
        /// The list of other user's skills approved by this user
        /// </summary>
        [InverseProperty("ApprovedBy")]
        public virtual ICollection<User_Skill> ApprovedSkills { get; set; }

        /// <summary>
        /// A collection of tasks that are lead by this user
        /// </summary>
        [InverseProperty("Leader")]
        public virtual ICollection<Task> LeaderOf { get; set; }

        /// <summary>
        /// A collection of tasks in which the user is a group member
        /// </summary>
        public virtual ICollection<Task> Tasks { get; set; }

        #region Personal informations
        /// <summary>
        /// The first name of the user
        /// </summary>
        [Required(ErrorMessage="Please enter a name")]
        [Display(Name = "First name")]
        public string FirstName { get; set; }

        /// <summary>
        /// The last name of the user
        /// </summary>
        [Required(ErrorMessage = "Please enter a last name")]
        [Display(Name = "Last name")]
        public string LastName { get; set; }

        /// <summary>
        /// The address (street name, house no., apt no., ...) of the user
        /// </summary>
        [Required(ErrorMessage = "Please enter an address")]
        [Display(Name = "Address")]
        public string Address { get; set; }

        /// <summary>
        /// The city of the user's address
        /// </summary>
        [Required(ErrorMessage = "Please enter a city")]
        [Display(Name = "City")]
        public string City { get; set; }

        /// <summary>
        /// The country of the user
        /// </summary>
        [Required(ErrorMessage = "Please enter a country")]
        [Display(Name = "Country")]
        public string Country { get; set; }

        /// <summary>
        /// The ZIP of the user's address
        /// </summary>
        [Required(ErrorMessage = "Please enter a zip code")]
        [Display(Name = "ZIP code")]
        public string ZIP { get; set; }

        /// <summary>
        /// The user's e-mail address
        /// </summary>
        [Required(ErrorMessage = "Please enter an e-mail address")]
        [RegularExpression("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}", ErrorMessage="Enter a valid e-mail address")]
        [Display(Name = "E-mail")]
        public string Email { get; set; }

        /// <summary>
        /// The main phone number of the user
        /// </summary>
        [Display(Name = "Phone")]
        public string Phone { get; set; }

        /// <summary>
        /// The position held by the user in the company. Eg: CEO, Software Engineer (senior), ...
        /// </summary>
        [Required(ErrorMessage = "Please enter a position")]
        [Display(Name = "Position in the company")]
        public string Position { get; set; }

        /// <summary>
        /// The personal ID of the user. Eg: SSN, PESEL, Passport No., ...
        /// </summary>
        [Required(ErrorMessage = "Please enter a personal id")]
        [Display(Name = "Personal identifiter")]
        public string PersonalIdentifier { get; set; }

        /// <summary>
        /// The user's date of birth
        /// </summary>
        [Required(ErrorMessage = "Please enter a date of birth")]
        [DataType(DataType.Date)]
        [Display(Name = "Date of birth")]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime DateOfBirth { get; set; }
        #endregion

        /// <summary>
        /// The time when the user was added to the system
        /// </summary>
        [Display(Name = "With us since")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime CreationDate { get; set; }

        /// <summary>
        /// The backin field for the status enumeration
        /// </summary>
        public int StatusRep
        {
            get { return (int)Status; }
            set { Status = (StatusEnum)value; }
        }

        /// <summary>
        /// The employment status of the user
        /// </summary>
        public StatusEnum Status { get; set; }

        /// <summary>
        /// A collection of the entered working hours
        /// </summary>
        public virtual ICollection<WorkingHoursEntry> WorkingHoursEntries { get; set; }

        /// <summary>
        /// A collection of the reports created by the user
        /// </summary>
        public virtual ICollection<ReportHeader> CreatedReports { get; set; }

        /// <summary>
        /// A collection of the reports approved by the user
        /// </summary>
        public virtual ICollection<ReportHeader> ApprovedReports { get; set; }
    }
}